package com.qujia.station.utils;


import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map.Entry;
import java.util.StringJoiner;
import java.util.TreeMap;

/**
 * @author zzq
 * @version 1.0.0
 * @Title {@link}
 * @Description
 * @date 2020/5/27
 */

public class SignUtils {

    public static String sign(TreeMap<String, String> param) {
        StringJoiner joiner = new StringJoiner("&");
        for (Entry<String, String> each : param.entrySet()) {
            joiner.add(StringUtils.join(each.getKey(), "=", each.getValue()));
        }

        return DigestUtils.md5Hex(joiner.toString());
    }

    public static boolean verify(TreeMap<String, String> param) {
        String originalSign = param.remove("sign");
        String sign = sign(param);
        //log.info("原密文 : [{}], MD5签名结果 : [{}]", originalSign, sign);
        boolean verify = StringUtils.equals(originalSign, sign);
        //log.info("签名验证结果: [{}]", verify ? "通过" : "不通过");
        return verify;
    }

}
